//
//  HorizontalTextsListsView.swift
//  AmountBody
//
//  Created by Qiu Jishuai on 2018/1/25.
//  Copyright © 2018年 RCMTM. All rights reserved.
//

import UIKit

class HorizontalTextsListsView: UIView {

    var contentInset: UIEdgeInsets = UIEdgeInsets(top: 25, left: 25, bottom: 25, right: 25)
    var maxWidth: CGFloat = UIScreen.main.bounds.width
    var font: UIFont = .systemFont(ofSize: 12)
    var textColor: UIColor = UIColor(hexString: "#666666")!
    var textBorderColor: UIColor = UIColor(hexString: "#666666")!
    var itemHeight: CGFloat = 30
    var itemSpacing: CGFloat = 20
//    var tolerableAdjustetValue: CGFloat = 3
    var textSpacing: CGFloat = 12
    var itemSpacingH: CGFloat = 20

    lazy var roundedBorderImage: UIImage = {
        let textBorderColor = UIColor(hexString: "#666666")!
        var image = UIImage(color: .white, size: CGSize(width: 30, height: self.itemHeight))!
        let scale = 1 / UIScreen.main.scale
        image = image.byRoundCornerRadius(8, borderWidth: scale, borderColor: textBorderColor)!
        image = image.stretch(withInsetsTop: 10, left: 10, bottom: 10, right: 10)
        return image
    }()

    var texts: [String] = [] {
        didSet {
            while buttons.count < texts.count {
                let button = UIButton(type: .system)
//                button.translatesAutoresizingMaskIntoConstraints = false
                button.setTitleColor(textColor, for: .normal)
                button.titleLabel?.font = font
                button.setBackgroundImage(roundedBorderImage, for: .normal)
                button.addTarget(self, action: #selector(buttonClicked(_:)), for: .touchUpInside)
                addSubview(button)
                buttons.append(button)
            }
            while buttons.count > texts.count {
                buttons.last?.removeFromSuperview()
                _ = buttons.removeLast()
            }
            var row: CGFloat = 0
            var x: CGFloat =  contentInset.left

            for (index, button) in buttons.enumerated() {
                button.setTitle(texts[index], for: .normal)
                button.sizeToFit()
                let buttonWidth = button.width + 2 * textSpacing

                //计算当前的累计宽度  如果该行累计宽度加上按钮的宽度大于 指定宽度则放到下一行
                if x + buttonWidth + itemSpacing > maxWidth { //换行
                    row += 1
                    x = contentInset.left //重新累积
                }
                let y = row * (itemHeight + itemSpacingH) + contentInset.top
                button.frame = CGRect(x: x, y: y, width: buttonWidth, height: itemHeight)
                x += (buttonWidth + itemSpacingH)
                button.tag = index
            }
            invalidateIntrinsicContentSize()
            superview?.setNeedsLayout()
            superview?.layoutIfNeeded()
        }
    }

    override func layoutSubviews() {
        super.layoutSubviews()


    }

    override var intrinsicContentSize: CGSize {
        return CGSize(width: width + contentInset.left + contentInset.right, height: (buttons.last?.frame.maxY ?? 0) + contentInset.top + contentInset.bottom)
    }

    private(set) var buttons: [UIButton] = []

    var buttonTapsAction: ((String) -> Void)?

    @objc func buttonClicked(_ sender: UIButton) {
        if let title = sender.title(for: .normal) {
            buttonTapsAction?(title)
        }
    }
}






